package com.zx.map.controller.timer;


import com.zx.map.dao.BalanceSummaryDao;
import com.zx.map.dao.BaseAcctInfoDao;
import com.zx.map.dao.ShareProfitLogDao;
import com.zx.map.dao.model.BalanceSummary;
import com.zx.map.dao.model.BaseAcctInfoExample;
import com.zx.map.dao.model.ShareProfitLog;
import com.zx.map.dao.model.ShareProfitLogExample;
import com.zx.map.service.BaseAcctInfoService;
import com.zx.map.service.ShareProfitLogService;
import com.zx.map.service.model.AgentInfoModel;
import com.zx.map.service.model.BaseAcctInfoModel;
import com.zx.map.service.model.MerchantInfoModel;
import com.zx.map.service.model.ShareProfitLogModel;
import com.zx.platform.utils.PmpUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

/**
 * Created by Jaelyn on 2017/1/19.
 *
 * 临时用于存储
 */
public class SaveHistoryDataTimer {
	private static Logger log = Logger
			.getLogger(SaveHistoryDataTimer.class);
	@Autowired
	private ShareProfitLogService shareProfitLogService;
	@Autowired
	private BaseAcctInfoService baseAcctInfoService;
	@Autowired
	private BalanceSummaryDao balanceSummaryDao;


	public static double accoutTotalAvlBal;
	public static double accoutTotalFrzBal;
	public static double shareProfitTotalBal;
	public static double shareProfitTotalAvlBal;
	public static double yesterdayTotalShareProfit;

	public void saveHistoryData(){
		log.info("保存昨日数据开始");
		try {

			Map map = baseAcctInfoService.queryPageByDomain(new MerchantInfoModel(), new BaseAcctInfoModel(), 1, 1);
			accoutTotalAvlBal = (Double) map.get("totalAvlbal");
			accoutTotalFrzBal = (Double) map.get("totalFrzbal");

			ShareProfitLog shareProfitLogModel = new ShareProfitLog();
			shareProfitLogModel.setPageSize(1);
			shareProfitLogModel.setPageNum(1);
			Map map2 = shareProfitLogService.queryShareProfitLog(null, new AgentInfoModel(),shareProfitLogModel);
			shareProfitTotalBal = (Double) map2.get("sprofitBal");
			shareProfitTotalAvlBal = (Double) map2.get("sprofitAvlAmt");

			//数据存入 BalanceSummary表
			BalanceSummary balanceSummary = new BalanceSummary();
			balanceSummary.setCreateDate(PmpUtils.dateToString(new Date(), "yyyy-MM-dd"));
			balanceSummary.setAccountBal(accoutTotalAvlBal);
			balanceSummary.setAccountFrzBal(accoutTotalFrzBal);
			balanceSummary.setShareBal(shareProfitTotalBal);
			balanceSummary.setShareAvlBal(shareProfitTotalAvlBal);
			balanceSummary.setYesterdayShare(yesterdayTotalShareProfit);
			balanceSummaryDao.add(balanceSummary);

			log.info("账户可用余额："+accoutTotalAvlBal);
			log.info("账户冻结余额："+accoutTotalFrzBal);
			log.info("分享收益总额："+shareProfitTotalBal);
			log.info("分享收益可用余额："+shareProfitTotalAvlBal);
		}catch (Exception e){
			log.error("保存昨日数据",e);
		}

		log.info("保存昨日数据结束");

	}

}
